package com.xusheng.cli.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.bankCardModule.BankCard;
import com.xusheng.cli.service.BankCardService;
import com.xusheng.cli.service.FrontUserService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 银行卡表(BankCard)表控制层
 *
 * @author whr
 * @since 2024-03-19 17:26:37
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user/v1/bankCard")
public class BankCardController {
    /**
     * 服务对象
     */
    @Resource
    private BankCardService bankCardService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private FrontUserService frontUserService;

    /**
     * 查询所有数据
     *
     * @param bankCard 查询实体
     * @return 所有数据
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @GetMapping("queryAll")
    public Result<List<BankCard>> queryAll(BankCard bankCard) {
        bankCard.setOwnerId(CurrentPlayerUserUtil.getUserId());
        return Result.success(bankCardService.list(new QueryWrapper<>(bankCard)));
    }

    /**
     * 分页查询所有数据
     *
     * @param page     分页对象
     * @param bankCard 查询实体
     * @return 所有数据
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @GetMapping("queryByPage")
    public Result<MyPage<BankCard>> queryByPage(MyPage<BankCard> page, BankCard bankCard) {
        return Result.success(bankCardService.page(page, new QueryWrapper<>(bankCard)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @GetMapping("queryOne/{id}")
    public Result<BankCard> selectOne(@PathVariable Serializable id) {
        return Result.success(bankCardService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param bankCard 实体对象
     * @return 新增结果
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody BankCard bankCard) {
//        String key = SMSVerificationCode.PHONE_PASSWORD_KEY + bankCard.getMobile();
      /*  String key = null;
        String c = redisTemplate.opsForValue().get(key);
        log.info("key={}, redis-code = {}, user-code={}", key, c, bankCard.getCode());
        if (StrUtil.isBlank(c) || !c.equals(bankCard.getCode())) {
            return Result.error("短信验证码不正确");
        }
        redisTemplate.delete(key);*/
        Long userId = CurrentPlayerUserUtil.getUserId();
    /*      FrontUser user = frontUserService.getById(userId);
      if (StrUtil.isBlank(user.getIdentityCard())) {
            return Result.error("请先实名认证");
        }
        AuthResult result = thiredpartyService.bankAuth(bankCard.getCardNo(), user.getIdentityCard(), bankCard.getMobile(), bankCard.getName());
        if (!BankAuthCode.C10000.code().equals(result.getCode())) {
            return Result.error("银行卡认证失败");
        }
        if (!AuthResult.State.SAME.code().equals(result.getData().getState())) {
            return Result.error("请检查银行卡信息是否正确");
        }*/
        bankCard.setOwnerId(userId);
        long count = bankCardService.count(new LambdaQueryWrapper<>(new BankCard()).eq(BankCard::getOwnerId, userId));
        bankCard.setCardDefault(count == 0);
        return Result.isSuccess(bankCardService.save(bankCard));
    }

    /**
     * 修改数据
     *
     * @param bankCard 实体对象
     * @return 修改结果
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody BankCard bankCard) {
        return Result.isSuccess(bankCardService.updateById(bankCard));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        BankCard bankCard = new BankCard();
        bankCard.setId(id);
        return Result.isSuccess(bankCardService.removeById(bankCard));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2024-03-19 17:26:37
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(bankCardService.batchDelete(idList, new BankCard()));
    }
}

